﻿//-----------------------------------------------------------------------
// <copyright file="ExplorerItemToImageSourceConverter.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>15/07/2012</date>
//-----------------------------------------------------------------------
using ConsoleHoster.Model.Entities;
using ConsoleHoster.Utilities;
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace ConsoleHoster.View.Converters
{
	[ValueConversion(typeof(ExplorerItem), typeof(ImageSource))]
	public class ExplorerItemToImageSourceConverter : IValueConverter
	{
		private readonly IExplorerItemIconProvider iconManager;

		public ExplorerItemToImageSourceConverter()
			: this(new ExplorerItemIconProviderFactory(), true)
		{
		}

		internal ExplorerItemToImageSourceConverter(ExplorerItemIconProviderFactory argFactory, bool argUseShellIcons)
			: this(new ExplorerIconManager(argFactory.Create(argUseShellIcons)))
		{
		}

		internal ExplorerItemToImageSourceConverter(IExplorerItemIconProvider argItemProvider)
		{
			if (argItemProvider == null)
			{
				throw new ArgumentNullException("argItemProvider");
			}

			this.iconManager = argItemProvider;
		}

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			ImageSource tmpResult = null;
			try
			{
				tmpResult = this.iconManager.GetIcon(value as ExplorerItem);
			}
			catch
			{
			}

			return tmpResult;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotSupportedException("Cannot convert back");
		}
	}
}